package com.bovink.util.sample;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bovink.util.library.PoweredAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 继承PoweredAdapter
 * 通过getCount传递item数量
 * 通过onCreateItemView传递布局
 * 通过onBindItemView绑定数据或行为
 *
 * @author bovink
 * @since 2016/8/18
 */
public class MyPoweredAdapter extends PoweredAdapter<MyPoweredAdapter.RecyclerViewItem> {
    LayoutInflater layoutInflater;
    ArrayList<String> strings;

    public MyPoweredAdapter(Context context, ArrayList<String> strings) {
        if (context == null || strings == null) {
            throw new NullPointerException();
        }
        layoutInflater = LayoutInflater.from(context);
        this.strings = strings;
    }

    @Override
    public RecyclerViewItem onCreateItemView(ViewGroup parent) {
        return new RecyclerViewItem(layoutInflater.inflate(R.layout.view_recycler_item, parent, false));
    }

    @Override
    public void onBindItemView(RecyclerViewItem hold, int position) {
        String text = strings.get(position) + (position + 1);
        hold.tv_item.setText(text);
    }

    @Override
    public int getCount() {
        // 数据的长度，不需要计算顶部和尾部，会根据setHeader和setFooter自动计算
        return strings.size();
    }

    public void addStrings(List<String> strings) {
        this.strings.addAll(strings);
    }

    public void clearStrings() {
        strings.clear();
    }

    /**
     * 子项目
     */
    public class RecyclerViewItem extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item)
        TextView tv_item;

        public RecyclerViewItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            tv_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println(tv_item.getText());
                }
            });
        }
    }
}
